package com.sghd.fight.utils;

/**
 * 伪随机函数
 */
public class Random {
    public Random(long longSeed) {
        seed = longSeed;
    }

    private long seed;

    private static final long multiplier = 0x5DEECE66DL;
    private static final int addend = 0xB;
    private static final long mask = (1L << 48) - 1;

    protected int next(int bits) {
        long nextSeed = (seed * multiplier + addend) & mask;
        seed = nextSeed;
        return (int) (nextSeed >>> (48 - bits));
    }

    public int nextInt(int bound) {
        if (bound <= 0)
            throw new IllegalArgumentException("n must be positive");
        int r = next(31);
        int m = bound - 1;
        if ((bound & m) == 0)
            r = (int) ((bound * (long) r) >> 31);
        else {
            for (int u = r; u - (r = u % bound) + m < 0; u = next(31)) ;
        }
        return r;
    }

    public long getSeed() {
        return seed;
    }

    public void setSeed(long seed) {
        this.seed = seed;
    }

}
